home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16771 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  54 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Niklas Mellin <niklas.mellin@sto.fdata.se>
  4. Subject: Re: disgust for goto's
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <316E1773.E87@sto.fdata.se>
  7. Date: Fri, 12 Apr 1996 08:42:27 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <4kjue7$6ug@bertrand.ccs.carleton.ca>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0 (WinNT; I)
  13. Organization: WM-data F÷rsvarsdata AB, Sweden
  14.  
  15. jean richard wrote:
  16. > Why is the goto command so looked down upon in any language??
  17. > I remember a class where if you used a goto, you failed the course. :-)
  18.  
  19. Because it can easily lead to unreadable spaghetti code. And there are
  20. usually more elegant ways to solve a problem than to use a goto.
  21.  
  22. void f()
  23. {
  24. loopStart:
  25.   // Do something
  26.   goto loopStart;
  27. }
  28.  
  29. is equivalent to
  30.  
  31. void f()
  32. {
  33.   for (;;)
  34.   {
  35.     // Do something
  36.   }
  37. }
  38.  
  39. Sometimes goto can be handy to exit nested loops (exiting
  40. a normal loop can be done with break), but if the exiting is
  41. due to an error condition it is IMHO more elegant to use a
  42. try - catch block around the loops instead. And writing code
  43. that must exit more than one nesting level in other than
  44. exceptional cases is usually bad coding I think.
  45.  
  46. Of cource there are exceptions from every thumb rule. But I
  47. have never had a need for goto on C++ compilers that can handle
  48. exceptions.
  49.  
  50. ---
  51. Niklas Mellin
  52.